//
//  KeyWindowPopAlterView.m
//  mmbang
//
//  Created by lihuaming on 15/9/8.
//  Copyright (c) 2015年 iyaya. All rights reserved.
//

#import "KeyWindowPopAlterView.h"
#import "AnimationFactory.h"

@implementation KeyWindowPopAlterView

+ (instancetype)instance {
    static dispatch_once_t onceToken;
    static KeyWindowPopAlterView *theInstance;
    dispatch_once(&onceToken, ^{
        theInstance = [[KeyWindowPopAlterView alloc] init];
    });
    return theInstance;
}

- (void)showAlterView:(UIView *)alterView {
    _popView = alterView;
    UIView *view=[[UIApplication sharedApplication] keyWindow];
    _maskView=[[UIView alloc]initWithFrame:view.bounds];
    _maskView.alpha=0.6f;
    _maskView.backgroundColor=[UIColor blackColor];
    [view addSubview:_maskView];
    alterView.center = view.center;
    
    
    [view addSubview:alterView];
    
    kWeakSelf
    _maskView.tappedBlock = ^(UITapGestureRecognizer*tapGestureRecognizer){
        if (weakSelf.tapSpaceBlock) {
            weakSelf.tapSpaceBlock(weakSelf);
        }
        [weakSelf close:weakSelf.popView];
    };
    [UIView animateWithDuration:0.5 animations:^{
        weakSelf.popView.alpha = 1.0;
        [weakSelf.popView.layer addAnimation:[AnimationFactory scaleAnimation:TRUE] forKey:@"LOGINVIEWWILLAPPEAR"];
    } completion:^(BOOL finished){
    }];

}

- (void)close:(UIView*)alterView{
    kWeakSelf
    [UIView animateWithDuration:0.3 animations:^{
        weakSelf.popView.alpha = 0.0;
        [weakSelf.popView.layer addAnimation:[AnimationFactory scaleAnimation:FALSE] forKey:@"LOGINVIEWWILLDISAPPEAR"];
    } completion:^(BOOL finished){
        if (weakSelf.dissPopAlterBlock) {
            weakSelf.dissPopAlterBlock(weakSelf);
        }
        [weakSelf.popView removeFromSuperview];
        [weakSelf.maskView removeFromSuperview];
    }];

}
@end
